
// 自定义的一些工具方法

var Af = {}

Af.rest = function(url, data, success, error){
	if(data == null) data = {}; // 若data为null,则发送一个默认JSON{}
	
	$.ajax({
		method: 'POST' ,  
		url: url ,
		contentType: 'application/json' ,  // HTTP请求中的 Content-Type
		data : JSON.stringify(data) ,      // HTTP请求正文 
		processData : false,               // 
		dataType: 'json',                  // HTTP应答按JSON处理
		success : success ,
		error : error ,
	})	
}

Af.rest2 = function(arg){
	if(arg.data == null) arg.data = {}; // 若data为null,则发送一个默认JSON{}
	
	$.ajax({
		method: 'POST' ,  
		url: arg.url ,
		contentType: 'application/json' ,  // HTTP请求中的 Content-Type
		data : JSON.stringify(arg.data) ,      // HTTP请求正文 
		processData : false,               // 
		dataType: 'json',                  // HTTP应答按JSON处理
		success : arg.success ,
		error : arg.error ,
	})	
}



/* 获取查询参数 */
Af.getUrlParam = function (name, defValue) 
{
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) 
    	return unescape(r[2]);
    else
    	return defValue;
}

// file , 必需
// url, 必需
// 事件回调 (可选) : complete , error, abort, progress
Af.upload = function( args ){
	if(args.file == null) throw 'Af.upload: 缺少 file 参数!';
	if(args.url == null)  throw 'Af.upload: 缺少 url 参数!';
	
	var form = new FormData();
	form.append('file', args.file); 
	// if(args.extra != null){ // 附加表单参数
	// 	for( var key in args.extra)	form.append(key, args.extra[key]);
	// }	
	
	var httpRequest = new XMLHttpRequest();
	
	// 指定一些回调方法
	var ctx = args;
	ctx.httpRequest = httpRequest;
	httpRequest.ctx = httpRequest.upload.ctx = ctx;
	httpRequest.addEventListener("load", Af.evt_upload_complete, false);
	httpRequest.addEventListener("error", Af.evt_upload_error, false);
	httpRequest.addEventListener("abort", Af.evt_upload_abort, false);				
	httpRequest.upload.addEventListener("progress", Af.evt_upload_progress, false);
				
	// 上传到后台			
	httpRequest.open("POST", args.url );
	httpRequest.send(form);			
	
}

Af.evt_upload_complete = function(evt)
{
	var ctx = this.ctx;  // this指向formRequest
	if(ctx.complete){
		ctx.complete( {
			responseText : evt.target.responseText
		} );
	}
};	
	 
Af.evt_upload_error = function (evt) 
{		
	var ctx = this.ctx;  // this指向formRequest
	if(ctx.error){
		ctx.error();
	}	
};

Af.evt_upload_abort  = function(evt) 
{
	var ctx = this.ctx;  // this指向formRequest
	if(ctx.abort){
		ctx.abort(  );
	}	
};

Af.evt_upload_progress = function(evt)
{
	var ctx = this.ctx;  // this指向formRequest.upload
	if(ctx.progress){
		ctx.progress( {
		  	loaded: evt.loaded ,
			total: evt.total ,
			percent : Math.round(evt.loaded * 100 / evt.total) ,
		} );
	}
};	


/* localStorage /sessionStorage */
/* value可以为 String , Number, Boolean , Object, Array*/
Af.putStorage = function (storage, key, value)
{
	if(value == null) return ;
	if(value.constructor == String)	value = "S#" + value;
	else if(value.constructor == Number) value = "N#" + value;
	else if(value.constructor == Boolean) value = "B#" + value;
	else value =  "J#" + JSON.stringify(value);	
	storage.setItem(key, value);
}
Af.getStorage = function(storage, key ,defValue)
{	
	var value = storage.getItem(key);
	if(value == null) return defValue ;	
	var type = value.substr(0,2);
	value = value.substr(2);
	if(type=="S#") return value;
	if(type=="N#") return Number(value);
	if(type=="B#") return value=="true";
	if(type=="J#") return JSON.parse(value);
	return defValue;
}
Af.putLocal = function(key, value)
{
	Af.putStorage(localStorage, key, value);
}
Af.getLocal = function(key, defValue)
{
	try{
		return Af.getStorage(localStorage, key, defValue);
	}catch(err){
		Af.log('getLocal:格式不对?' + key);
		return defValue;
	}
}
/* sessionStorage */
Af.putSession = function(key, value)
{
	Af.putStorage(sessionStorage, key, value);
}
Af.getSession = function(key, defValue)
{
	try{
		return Af.getStorage(sessionStorage, key, defValue);
	}catch(err){
		Af.log('getSession:格式不对?' + key);
		return defValue;	
	}
}



